package views;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import common.Contexto;

import controllers.IErrorObserver;
import controllers.LoginController;


@SuppressWarnings("serial")
public class LoginPanel extends JPanel implements IErrorObserver {

	private JLabel nombreUsuarioL, contrasenyaL;

	private JTextField nombreUsuarioT;
	private JPasswordField contrasenyaT;
	private JButton loginB;

	private LoginController controller;
	
		/**
	 * main de prueba para la pantalla de socio
	 * @param args
	 */
	public static void main(String[] args) {
		LoginPanel socioPanel = new LoginPanel();
		socioPanel.showPanel();
	}
	
	public LoginPanel() {
		initGui();
		layoutComponents();
		initActions();
		
		this.controller = new LoginController();
		
		this.controller.agregarErrorObserver(this);
		
		Contexto.inicializarModeloDatos();
	}
	
	private void initGui() {
		nombreUsuarioL = new JLabel("Usuario");
		contrasenyaL = new JLabel("Clave");
		
		loginB = new JButton("Iniciar");
		
		nombreUsuarioT = new JTextField(10);
		contrasenyaT = new JPasswordField(10);
	}

	private void layoutComponents() {
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST ;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(0, 2, 2, 4);
        add(nombreUsuarioL, c);

        c.gridy = GridBagConstraints.RELATIVE;
        add(contrasenyaL, c);
        
        c.gridx = 1;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST ;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(0, 2, 2, 4);
        add(nombreUsuarioT, c);

        c.gridy = GridBagConstraints.RELATIVE;
        add(contrasenyaT, c);
        
        c.gridx = 1;
        c.gridy = 7;
        c.insets = new Insets(20, 0, 10, 0);
        
        add(loginB, c);

	}
	
	private void initActions() {
		loginB.setAction(new AbstractAction("Iniciar"){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(validateFields()) {
					
					boolean loggedIn = controller.login(nombreUsuarioT.getText(),String.valueOf(contrasenyaT.getPassword()));
					if(loggedIn) {
						SwingUtilities.windowForComponent(LoginPanel.this).dispose();
						controller.mostrarPantallaPrincipal();
					}
				}else {
					notificarError("Por favor complete nombre de usuario y clave");
				}
			}			
		});
		
		contrasenyaT.addActionListener(new AbstractAction("Iniciar"){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(validateFields()) {
					
					boolean loggedIn = controller.login(nombreUsuarioT.getText(),String.valueOf(contrasenyaT.getPassword()));
					if(loggedIn) {
						SwingUtilities.windowForComponent(LoginPanel.this).dispose();
						controller.mostrarPantallaPrincipal();
					}
				}else {
					notificarError("Por favor complete nombre de usuario y clave");
				}
			}			
		});
	}
	
	private boolean validateFields() {	
		if(nombreUsuarioT.getText().length() > 0 && contrasenyaT.getPassword().length > 0){
			return true;
		}
		
		return false;
	}

	@Override
	public void notificarError(String error) {
		Contexto.displayErrorMessage(this, error);
	}
	
	public void showPanel() {
		final JFrame mainFrame = new JFrame();
        mainFrame.setTitle("Login");
        mainFrame.getContentPane().add(this);
        mainFrame.setPreferredSize(new Dimension(300,240));
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
	}
}
